package com.hn.kiss.modules.sys.service.impl;

import com.hn.kiss.config.common.ParmException;
import com.hn.kiss.modules.sys.dto.RegisterDTO;
import com.hn.kiss.modules.sys.entity.User;
import com.hn.kiss.modules.sys.service.ILoginService;
import com.hn.kiss.modules.sys.service.IUserRoleService;
import com.hn.kiss.modules.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LoginServiceimpl implements ILoginService {
    @Value("${project.rolecode.temp}")
    private String tempRole;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public boolean register(RegisterDTO registerDTO) throws ParmException {
        User user = registerDTO.convertToUser();
        return userService.add(user);
    }

    @Transactional
    @Override
    public boolean addTempUser(String email) {
        User user = new User().setEmall(email);
        userService.save(user);
        userRoleService.setDefaultRole(user.getId(), tempRole);
        return true;
    }
}
